package com.wzl.responsitory;

import com.wzl.dao.SysDictDataMapper;
import com.wzl.entity.SysDictData;
import com.wzl.entity.vo.SysDictDataVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.*;
import java.util.stream.Collectors;

@Component
public class SelectCode {
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 获取该分页所有码值数据并处理
     * @param listCode
     * @return
     */
    public Map<String, List<SysDictDataVO>> dealAllCode(List<String> listCode) {
        Map<String, List<SysDictDataVO>> codeMap = new HashMap<>();
        // 获取所有码值数据
        List<SysDictData> listAllValue = sysDictDataMapper.optionListCode(listCode);
        // copy字段
        List<SysDictDataVO> dicData = listAllValue.stream().map(oldValue-> {
            SysDictDataVO newValue = new SysDictDataVO();
            newValue.setLabel(oldValue.getLabel());
            newValue.setTypeCode(oldValue.getTypeCode());
            newValue.setValue(oldValue.getValue());
            return newValue;
        }).collect(Collectors.toList());
        listCode.forEach(codeDic -> {
            List<SysDictDataVO> tempCodeList = new ArrayList<>();
            dicData.forEach(codeInfo -> {
                if (StringUtils.equals(codeDic, codeInfo.getTypeCode())) {
                    tempCodeList.add(codeInfo);
                }
                codeMap.put(codeDic, tempCodeList);
            });
        });
        return codeMap;
    }
}
